<?php

class GameRound {
  /** @var integer */
  public $id;
  /** @var integer */
  public $gameId;
  /** @var integer */
  public $dealerId;
  /** @var string */
  public $trump;
  /** @var integer */
  public $roundAmount;
  /** @var integer */
  public $roundNumber;
  /** @var string */
  public $status;
  /** @var Array<Card> */
  public $deck;

  public $deckType;
  
  /* -------- CONSTANTS -------- */
  const STATUS_NEW     = 'new';
  const STATUS_STARTED = 'started';
  const STATUS_ENDED   = 'ended';

  /** @var Array<String> - dictionary of all statuses and their titles */
  static $STATUS_TITLES = array(
    self::STATUS_NEW     => 'New',
    self::STATUS_STARTED => 'Started',
    self::STATUS_ENDED   => 'Ended',
  );
  /* -------- CONSTANTS -------- */
  

  /* -------- METHODS -------- */
  /**
   * Loads a Game object from database or creates a new Game instance if no $id
   * paramenter given.
   * @param integer $id
   */
  public function __construct($id = null, $gameId = null)
  {
    $this->Init();

    if (!$this->Load($id, $gameId))
      $this->Init();
  }

  /**
   * sets default values to class's properties.
   */
  private function Init()
  {
    $this->id = -1;
    $this->gameId = -1;
    $this->dealerId = -1;
    $this->status = self::STATUS_NEW;
  }

  /**
   * Loads data by querying database by id OR email
   * @param integer $id
   * @return bool true if data was successfully loaded, otherwise false
   */
  private function LoadData($id = null)
  {
    if (!is_numeric($id))
      return false;
    else
    {
      $gameRounds = DB::table('game_rounds')->where('id', '=', $id)->first();
      if (count($gameRounds) == 0)
        return false;
    }
    $this->SetPropertyValues($gameRounds);

    return true;
  }
  
  /**
   * Loads GameRound from database
   * @param integer $id - round id
   */
  private function Load($id = null)
  {
    if (!$this->LoadData($id))
      return false;

    return true;
  }


  /**
   * Populates basic model properties with data extracted from database.
   * @param array $data - dictionary of (<table column name> => <value>)
   */
  private function SetPropertyValues(&$data)
  {
    $this->id          = $data->id;
    $this->gameId      = $data->game_id;
    $this->dealer_id   = $data->dealer_id;
    $this->trump       = $data->trump;
    $this->roundAmount = $data->round_amount;
    $this->roundNumber = $data->round_number;
    $this->status      = $data->status;
    $this->deck        = json_decode($data->deck, true);
  }
  
  /**
   * Saves to database.
   * @return <type> 
   */
  public function Save()
  {
    $values = array();
    
    $values['game_id']      = intval($this->gameId);
    $values['dealer_id']    = intval($this->dealerId);
    $values['trump']        = $this->trump;
    $values['round_amount'] = $this->roundAmount;
    $values['round_number'] = $this->roundNumber;
    $values['status']       = $this->status;
    
    $oldGameRound = new GameRound($this->id);
    if ($oldGameRound->id && $oldGameRound->id > 0)
    {
      $res = DB::table('game_rounds')
              ->where('id', '=', $oldGameRound->id)
              ->update($values);
    }
    else
      $res = DB::table('game_rounds')->insert($values);
  }


  /**
   * Loads GameRounds from database.
   * @param integer $gameId - game id
   * @return Array<GameRound> $orders
   */
  public static function LoadGameRounds($gameId)
  {
    $rows = DB::table('game_rounds')->where('game_id', '=', $gameId)->get();

    for ($i = 0; $i < count($rows); $i++)
    {
      $gameRound = new GameRound();
      $gameRound->SetPropertyValues($rows[$i]);
      $gameRounds[] = $gameRound;
    }

    return $gameRounds;
  }
  
  public function SetGameRoundTrump($card)
  {
    if (is_numeric($card->id) && in_array($card->suit, Card::$SUIT_NAMES))
    {
      $this->trump = $card->suit;
      $this->Save();
    }
    else
      return false;

  }

  private function LoadGameRoundDeck($isSmallDeck = false)
  {
    $defaultDeck = Card::GetDeck($isSmallDeck);
    $roundDeck = $this->deck;
    $deck = array();
    $order = 0;

    foreach ($roundDeck as $key => $status)
    {
      $card = $defaultDeck[$key];
      $card->status = $status;
      $card->order = $order++;
      $deck[] = $card;
    }

    return $deck;
  }

  public function GetGameRoundDeck($isSmallDeck = false)
  {
    if ($this->deck === null)
      $deck = Card::GetShuffledDeck($isSmallDeck);
    else
      $deck = GameRound::LoadGameRoundDeck($isSmallDeck);

    return $deck;
  }

  public function SaveRoundDeck($deck)
  {
    if (!is_numeric($this->id))
      return;

    foreach($deck as $card)
      $roundDeck[$card->id] = $card->status;

    DB::table('game_rounds')->insert('deck', json_encode($roundDeck));
  }
  
}
?>
